<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0, minimum-scale=1.0, user-scalable=0, initial-scale=1.0, width=device-width"/>
    <meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
    <title>Hello APP</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui-pull-refresh.css" />
    <link rel="stylesheet" type="text/css" href="../css/reset.css" />
    <link rel="stylesheet" type="text/css" href="../css/style.css" />

</head>
<body>

  <section class="aui-refresh-content">
    <section class="aui-card-list" >
      <div id="main" ></div>
    </section>
  </section>
  <script id="template" type="text/x-dot-template">
    {{for(var prop in it){}}
    <div class="aui-hr"></div>
     <div class="aui-info aui-margin-t-10 aui-margin-bß-10 aui-padded-l-10 aui-padded-r-10" tapmode="hover" onclick="fnedit('{{=it[prop].code}}')">
         <div class="aui-info-item">
              <img src="../image/ic-barcode@32.png" />
               <span class="aui-margin-l-5">{{=it[prop].code}}</span>
         </div>
         <div class="aui-info-item">{{=it[prop].create_time}}</div>
     </div>
    {{}}}
  </script>

</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/aui-pull-refresh.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/zepto.min.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script type="text/javascript" src="../script/aui-toast.js"></script>
<script type="text/javascript" src="../script/aui-dialog.js"></script>

<script trpe="text/javascript">
  var user,barcode,codeData=[],UIListView,toast,dialog;
  apiready =function(){
    loadData();
    user=$api.getStorage(cacheUser);
    // $api.setStorage(codeDataKey,[
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'},
    //   {code:'97947843948374',create_time:'2017-08-23'}]);
      codeData =$api.getStorage(codeDataKey);
      toast = new auiToast({});
     dialog = new auiDialog({});
      initScan();
      api.addEventListener({
          name: 'clearEvent'
      }, function(ret, err){
        $("#main").empty();
        loadData();
      });

      api.addEventListener({
          name: 'stopDecodeEvent'
      }, function(ret, err){
        stopDecode();
      });


      //下拉刷新
      var pullRefresh = new auiPullToRefresh({
        container:document.querySelector('.aui-refresh-content'),
        triggerDistance:100
      },function(ret){
        console.info('ret'+JSON.stringify(ret));
          if(ret.status=='success'){
            codeData =$api.getStorage(codeDataKey);
            let main = document.getElementById('main');
            if(codeData){
              let lis =pagination(1,6,codeData);
               for(let i= 0,len = lis.length;i<len;i++){

                 var html = '<div class="aui-hr"></div><div class="aui-info aui-margin-t-10 aui-margin-bß-10 aui-padded-l-10 aui-padded-r-10" '+
                 'tapmode="hover" onclick="fnedit(\''+lis[i].code+'\')">'+
                     '<div class="aui-info-item">'+
                          '<img src="../image/ic-barcode@32.png" />'+
                           '<span class="aui-margin-l-5">'+lis[i].code+'</span>'+
                    ' </div>'+
                    ' <div class="aui-info-item">'+lis[i].create_time+'</div>'+
                 '</div>'+
                ' <div class="aui-hr"></div>';
               };
              main.insertAdjacentHTML('afterbegin',html);
              pullRefresh.cancelLoading();
            }
          }
      });

  };



 //scan init
 function initScan(){
     //用户没有登入
     if(!user){
        api.closeToWin({
            name: 'root'
        });
     }else{
       //开启扫描等待连接
       barcode =api.require('barcode');
       var param = {msg:"barcode!"};
       barcode.initScan(param,function(ret,err){
          if(ret){
            let code = ret.barcodeStr;
            console.info(code);
            if(checkeNotSame(code)){
              codeData =$api.getStorage(codeDataKey);
              if(!codeData){
                codeData=[];
              }
              let itemcode={code:code,create_time:new Date().format("yyyy-MM-dd hh:mm:ss")};
              codeData.push(itemcode);
              $api.setStorage(codeDataKey,codeData);
              codeData =$api.getStorage(codeDataKey);
              toast.custom({
                title:'扫描成功',
                html:'<span style="font-size:50">'+codeData.length+'</span>',
                duration:2000
              });

              api.sendEvent({
                  name: 'calcNumEvent',
              });

              var codehtml = '<div class="aui-hr"></div><div class="aui-info aui-margin-t-10 aui-margin-bß-10 aui-padded-l-10 aui-padded-r-10" '+
              'tapmode="hover" onclick="fnedit(\''+itemcode.code+'\')">'+
                  '<div class="aui-info-item">'+
                       '<img src="../image/ic-barcode@32.png" />'+
                        '<span class="aui-margin-l-5">'+itemcode.code+'</span>'+
                 ' </div>'+
                 ' <div class="aui-info-item">'+itemcode.create_time+'</div>'+
              '</div>'+
             ' <div class="aui-hr"></div>';

               let main = document.getElementById('main');
              $("#main").append(codehtml);

              //  UIListView = api.require('UIListView');
              //  UIListView.insertItem({
              //      index: 0,
              //      data: {
              //        uid: code,
              //        imgPath: 'widget://image/ic-barcode@32.png',
              //        title: code
              //      }
              //  }, function(ret, err){
              //      if( ret ){
              //       //  api.toast({
              //       //      msg: codeData.length,
              //       //      duration: 2000,
              //       //      location: 'midde'
              //       //  });
              //      }else{
              //         console.info( JSON.stringify( err ) );
              //      }
              //  });

            }
          }
       });
     }
 };

//loadData come on!
function loadData(){
     codeData =$api.getStorage(codeDataKey);
     if(codeData){
       let array =pagination(1,6,codeData);
       let evalText =doT.template($("#template").text());
       $("#main").append(evalText(array));
     }



};


//checke not same come on!
function checkeNotSame(code){
   codeData =$api.getStorage(codeDataKey);
   if(codeData){
     for(let i = 0,len =codeData.length;i<len;i++){
         if(codeData[i].code===code){
           toast.fail({
              title:'条码已经存在',
              duration:2000
           });
           return false;
         }
     };
   }
   return true;
}


Date.prototype.format = function(fmt) {
   var o = {
      "M+" : this.getMonth()+1,                 //月份
      "d+" : this.getDate(),                    //日
      "h+" : this.getHours(),                   //小时
      "m+" : this.getMinutes(),                 //分
      "s+" : this.getSeconds(),                 //秒
      "q+" : Math.floor((this.getMonth()+3)/3), //季度
      "S"  : this.getMilliseconds()             //毫秒
  };
  if(/(y+)/.test(fmt)) {
          fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
  }
   for(var k in o) {
      if(new RegExp("("+ k +")").test(fmt)){
           fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
       }
   }
  return fmt;
};

// array pagnation come on!
function pagination(pageNo,pageSize,array){
  var offset = (pageNo-1)*pageSize;
  return (offset+pageSize >= array.length)?array.slice(offset,array.length):array.slice(offset,offset+pageSize);
};

function fnedit(code){
     dialog.alert({
       title:"温馨提示",
       msg:"是否删除",
       buttons:['取消','删除']
     },function(ret){
        if(ret.buttonIndex==2){
        }
     });

}

function stopDecode(){
        console.info('-------关闭-------');
       barcode = api.require('barcode');
       barcode.stopDecode();
 }

</script>
</html>
